package com.book.settings.controller;

import com.book.common.Result;
import com.book.settings.pojo.User;
import com.book.settings.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.RequestScope;

import java.util.List;

@RestController
public class UserController {


    @Autowired
    private UserService userService;
    @PostMapping("/settings/admin/user/add.do")
    public Result addUser(@RequestBody User user){
        int addUserResult = userService.addUser(user);
        if(addUserResult==1){
            return Result.success();
        }else if(addUserResult==-1){
            return Result.error("添加的用户已经存在");
        }else{
            return Result.error("系统错误");
        }
    }

    @GetMapping("/settings/admin/user/search.do")
    public Result searchUser( User user){
        PageInfo<User> userPageInfo = userService.getListUser(user);
        int countListUser = userService.getCountListUser(user);
        return Result.successUsers(userPageInfo.getList(),countListUser);
    }

    @PostMapping("/settings/admin/user/del.do")
    public Result deleteUserById(@RequestBody User user){
        int deleteUserResult = userService.deleteUserById(user);
        if(deleteUserResult==1){
            return Result.success();
        }else if(deleteUserResult==-1){
            return Result.error("用户借阅的书籍还未完全归还");
        }else{
            return Result.error("未找到要删除的用户");
        }
    }

    @PostMapping("/settings/admin/user/update.do")
    public Result updateUserById(@RequestBody User user){
        int updateUserById = userService.updateUserById(user);
        if(updateUserById==1){
            return Result.success();
        }else{
            return Result.error("未找到要修改的用户");
        }
    }

    @GetMapping("/settings/admin/user/searchall.do")
    public Result searchAllUser( User user){
        PageInfo<User> userPageInfo = userService.getListALlUser(user);
        int countListAllUser = userService.getCountListAllUser(user);
        return Result.successUsers(userPageInfo.getList(),countListAllUser);
    }

    @GetMapping("/settings/admin/user/searchbyid.do")
    public User searchUserById(String id){
        User user = userService.searchUserById(id);
        return user;
    }

    @GetMapping("/settings/admin/user/searchbyact.do")
    public User searchUserByAct(String act){
        User user = userService.searchUserByAct(act);
        return user;
    }

}
